package com.spogher.bsc.pages;

import java.util.Date;
import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.datetime.PatternDateConverter;
import org.apache.wicket.datetime.markup.html.basic.DateLabel;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.spogher.bsc.data.dao.interfaces.ChampionshipDao;
import com.spogher.bsc.data.dataobjects.Championship;
import com.spogher.components.textfields.DatePickerTextField;

public class ChampionshipPage extends BscTemplatePage {
	
	@SpringBean
	private ChampionshipDao championshipDao;
	
	public ChampionshipPage(final PageParameters pp) {
		super();
		Form<Championship> championshipForm = new Form<Championship>("championshipForm", new CompoundPropertyModel<Championship>(new Championship()));
		championshipForm.add(new TextField<String>("name").setRequired(true));
		championshipForm.add(new DatePickerTextField("startDate").setRequired(true));
		championshipForm.add(new DatePickerTextField("endDate").setRequired(true));
		
		final WebMarkupContainer wmc = new WebMarkupContainer("listContainer");
		
		wmc.add(new ListView<Championship>("list", new PropertyModel<List<Championship>>(this, "championshipDao.findAll")){

			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Championship> item) {
				Championship championship = item.getModelObject();
				item.add(new Label("championshipName", championship.getName()));
				PatternDateConverter dateConverter = new PatternDateConverter("dd MMMM yyyy", true);
				DateLabel startDateLabel = new DateLabel("championshipStartDate", new PropertyModel<Date>(championship, "startDate"), dateConverter);
				DateLabel endDateLabel = new DateLabel("championshipEndDate", new PropertyModel<Date>(championship, "endDate"), dateConverter);
				item.add(startDateLabel);
				item.add(endDateLabel);
			}
			
		});
		wmc.setOutputMarkupId(true);
		add(wmc);
		
		championshipForm.add(new AjaxSubmitLink("submit"){
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Championship championship = (Championship) form.getModelObject();
				Championship newChampionship = new Championship();
				newChampionship.setName(championship.getName());
				newChampionship.setStartDate(championship.getStartDate());
				newChampionship.setEndDate(championship.getEndDate());
				championshipDao.save(newChampionship);
				target.addComponent(wmc);
			}
		});
		
		
		add(championshipForm);
		
	}	
	
}
